#include "../lv_project_inc/lvgl_overview.h"

//右上角标签
void lvgl_layout_topbar(lv_obj_t *obj)
{
    lv_obj_set_size(obj,100,35);
    lv_obj_align(obj,LV_ALIGN_TOP_RIGHT,0,0);
    lv_obj_set_style_pad_all(obj,0,0);
    lv_obj_set_style_border_width(obj,0,0);
    lv_obj_set_style_bg_color(obj,lv_color_hex(0x4F4F4F),0);

    lv_label_t *label_topbar_ba = lv_label_create(obj);
    lv_obj_align(label_topbar_ba,LV_ALIGN_RIGHT_MID,-5,0);
    lv_label_set_text(label_topbar_ba,LV_SYMBOL_BATTERY_FULL);
    lv_obj_set_style_text_font(label_topbar_ba,&lv_font_montserrat_24,0);
    lv_obj_set_style_text_color(label_topbar_ba,lv_color_hex(0xffffff),0);

    lv_label_t *label_topbar_wifi = lv_label_create(obj);
    lv_obj_align_to(label_topbar_wifi,label_topbar_ba,LV_ALIGN_OUT_LEFT_MID,-10,-2);
    lv_label_set_text(label_topbar_wifi,LV_SYMBOL_WIFI);
    lv_obj_set_style_text_font(label_topbar_wifi,&lv_font_montserrat_18,0);
    lv_obj_set_style_text_color(label_topbar_wifi,lv_color_hex(0xffffff),0);
}

//总览布局
void lvgl_layout_overview(lv_obj_t *obj)
{
    lv_obj_set_size(obj,LV_HOR_RES,LV_VER_RES);
    lv_obj_set_style_pad_all(obj,0,0);
    lv_obj_set_style_border_width(obj,0,0);
    lv_obj_set_style_radius(obj,0,0);
    lv_obj_set_style_bg_color(obj,lv_color_hex(0x4F4F4F),0);
}

//向云平台发送数据的线程
void* lvgl_send(void *arg)
{
    while(1)
    {

        char data[1024] = {0};
        overview_send_fomat_data(data);
        
        printf("%s\n",data);

        int ret = send(fd_overview_socket,data,strlen(data),0);
        if(ret == -1)
            perror("send");
        printf("ret = %d\n",ret);

        sleep(5);
    }
}

//从云平台接收数据的线程
void* lvgl_recv(void *arg)
{
    while(1)
    {
        char data[1024] = {0};

        int ret = recv(fd_overview_socket,data,sizeof(data),0);
        printf("ret = %d msg = %s\n",ret,data);
        
        if(ret == 3)
            continue;

        overview_recv_data_handle(data);
    }
}

//格式化发送到云平台的数据
void overview_send_fomat_data(char *str)
{
    cJSON *root = cJSON_CreateObject();
    cJSON_AddStringToObject(root,"temperature",text_label_temp);
    cJSON_AddStringToObject(root,"humidity",text_label_hum);
    cJSON_AddStringToObject(root,"air_quality",text_label_airq);
    cJSON_AddBoolToObject(root,"led_living_room",state_led_lroom);
    cJSON_AddBoolToObject(root,"led_master_room",state_led_mroom);
    cJSON_AddBoolToObject(root,"led_secondary_room",state_led_sroom);
    cJSON_AddBoolToObject(root,"led_bath_room",state_led_broom);
    cJSON_AddBoolToObject(root,"state_beep",state_beep);
    cJSON_AddBoolToObject(root,"state_smoke",state_smoke);

    strcpy(str,cJSON_Print(root));
    cJSON_Delete(root);
}

//处理从云平台接收到的数据
void overview_recv_data_handle(char *str)
{
    cJSON *root = cJSON_Parse(str);
    bool value_command = root->child->type == cJSON_True ? true : false;
    
    char *name_command = root->child->string;

    if(!strcmp(name_command,"led_liviing_room")) {lvgl_led_controll_handle(led_lroom_b,led_lroom_s,7,&state_led_lroom,value_command);}
    else if(!strcmp(name_command,"led_master_room")) {lvgl_led_controll_handle(led_mroom_b,led_mroom_s,8,&state_led_mroom,value_command);}
    else if(!strcmp(name_command,"led_secondary_room")) {lvgl_led_controll_handle(led_sroom_b,led_sroom_s,9,&state_led_sroom,value_command);}
    else if(!strcmp(name_command,"led_bath_room")) {lvgl_led_controll_handle(led_broom_b,led_broom_s,10,&state_led_broom,value_command);}

    cJSON_Delete(root);
}

//总览
void lvgl_overview(lv_obj_t *obj)
{
    lv_obj_t *layout_overview = lv_obj_create(obj);
    lvgl_layout_overview(layout_overview);

    lv_obj_t *layout_led = lv_obj_create(layout_overview);
    lvgl_layout_led(layout_led);

    lv_obj_t *layout_weather = lv_obj_create(layout_overview);
    lvgl_layout_weather(layout_weather);

    lv_obj_t *layout_weather_fut = lv_obj_create(layout_overview);
    lvgl_layout_weather_fut(layout_weather_fut);

    lv_obj_t *layout_timer = lv_obj_create(layout_overview);
    lvgl_layout_timer(layout_timer);

    lv_obj_t *layout_hum = lv_obj_create(layout_overview);
    lvgl_layout_hum(layout_hum);

    lv_obj_t *layout_temp = lv_obj_create(layout_overview);
    lvgl_layout_temp(layout_temp);

    lv_obj_t *layout_ariq = lv_obj_create(layout_overview);
    lvgl_layout_airq(layout_ariq);

    lv_obj_t *layout_smoke = lv_obj_create(layout_overview);
    lvgl_layout_smoke(layout_smoke);

    lv_obj_t *layout_call = lv_obj_create(layout_overview);
    lvgl_layout_call(layout_call);

    lv_obj_t *layout_topbar = lv_obj_create(layout_overview);
    lvgl_layout_topbar(layout_topbar);

    fd_overview_socket = client_run();
    if(fd_overview_socket == -1)
    {
        perror("client_run error");
        return -1;
    }

    pthread_t thread_send;
    pthread_create(&thread_send,NULL,lvgl_send,NULL);
    
    pthread_t thread_recv;
    pthread_create(&thread_recv,NULL,lvgl_recv,NULL);
}
